package com.dd.machine.main.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.baidu.tts.client.SpeechSynthesizer;
import com.dd.machine.base.app.MyApplication;
import com.dd.machine.main.commons.SPUtils;
import com.dd.machine.main.commons.VoiceUtils;
import com.dd.machine.main.user.BindPhoneActivity;
import com.dd.machine.main.user.GoodsKindActivity;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by Administrator on 2017/7/13.
 */

public class MyReceiver extends BroadcastReceiver {
    private static final String TAG = "JPush";
    private static String command = ""; //标识号
    private static SpeechSynthesizer mSpeechSynthesizer;



    @Override
    public void onReceive(Context context, Intent intent) {
        VoiceUtils voiceUtils = MyApplication.getInstances().getVoiceUtils();
        mSpeechSynthesizer = voiceUtils.getSyntheszer();
        Bundle bundle = intent.getExtras();
        Log.d(TAG, "[MyReceiver] onReceive - " + intent.getAction() + ", extras: "
                + printBundle(bundle,context));

//        Toast.makeText(context,printBundle(bundle,context),Toast.LENGTH_SHORT).show();

    }
    // 打印所有的 intent extra 数据
    private static String printBundle(Bundle bundle,Context context) {
        StringBuilder sb = new StringBuilder();
        for (String key : bundle.keySet()) {
            if (key.equals(JPushInterface.EXTRA_NOTIFICATION_ID)) {
                sb.append("\nkey:" + key + ", value:" + bundle.getInt(key));
            }else if(key.equals(JPushInterface.EXTRA_CONNECTION_CHANGE)){
                sb.append("\nkey:" + key + ", value:" + bundle.getBoolean(key));
            } else if (key.equals(JPushInterface.EXTRA_EXTRA)) {
                if (TextUtils.isEmpty(bundle.getString(JPushInterface.EXTRA_EXTRA))) {
                    Log.i(TAG, "This message has no Extra data");
                    continue;
                }

                try {
                    JSONObject json = new JSONObject(bundle.getString(JPushInterface.EXTRA_EXTRA));
                    Iterator<String> it =  json.keys();

                    while (it.hasNext()) {
                        String myKey = it.next().toString();
                        sb.append("\nkey:" + key + ", value: [" +
                                myKey + " - " +json.optString(myKey) + "]");
                        command = json.optString("command");

                        if ("THIRD_USERINFO".equals(command)) {
                            String id = json.optString("userId");
                            String openId = json.optString("openId");
                            String platform = json.optString("platform");

                            Log.e("==id==id",id);
                            Log.e("==id==openId",openId);
                            Log.e("==id==platform",platform);
                            if (SPUtils.canReceiver(context)) {
                                if ("".equals(id)||"0".equals(id)) {
                                    Intent intent = new Intent(context, BindPhoneActivity.class);
                                    Bundle bundleTo = new Bundle();
                                    bundleTo.putString("openId",openId);
                                    bundleTo.putString("platform",platform);
                                    intent.putExtras(bundleTo);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                                    context.startActivity(intent);
                                }else {
                                    Intent intent = new Intent(context, GoodsKindActivity.class);
                                    Bundle bundle1 = new Bundle();
                                    bundle1.putString("userId",id);
                                    intent.putExtras(bundle1);
                                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
                                    context.startActivity(intent);
                                }

                                Intent localIntnet = new Intent("receiver");
                                LocalBroadcastManager.getInstance(context).sendBroadcast(localIntnet);
                            }

                        }


                    }
                } catch (JSONException e) {
                    Log.e(TAG, "Get message extra JSON error!");
                }

            } else {
                sb.append("\nkey:" + key + ", value:" + bundle.getString(key));
            }
        }
        return sb.toString();
    }

}
